<template>
  <div class="app-container">
    <div class="search">
      <el-form
        ref="queryForm"
        class="queryForm"
        :inline="true"
        :model="formInline"
        label-width="auto"
      >
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item
              label="风险编号"
              class="formItem"
            >
              <el-input
                v-model="formInline.fxCode"
                placeholder="请输入风险编号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="风险名称"
              class="formItem"
            >
              <el-input
                v-model="formInline.fxxlName"
                placeholder="请输入风险名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="风险类型"
              class="formItem"
            >
              <el-select
                v-model="formInline.fxdlCode"
                placeholder="请选择风险类型"
                clearable
                class="w100"
                @change="pageNumChang"
              >
                <el-option
                  v-for="(item, index) in signTypeOption"
                  :label="item.label"
                  :value="item.value"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item
              label="风险等级"
              class="formItem"
            >
              <el-select
                v-model="formInline.fxdjCode"
                placeholder="请选择风险等级"
                clearable
                class="w100"
                @change="pageNumChang"
              >
                <el-option
                  v-for="(item, index) in signLiveOption"
                  :label="item.label"
                  :value="item.value"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="状态"
              class="formItem"
            >
              <el-select
                v-model="formInline.qyStatus"
                placeholder="请选择状态"
                clearable
                class="w100"
                @change="pageNumChang"
              >
                <el-option
                  v-for="(Labe_status, index) in LabeStatusOption"
                  :label="Labe_status.label"
                  :value="Labe_status.value"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleSearch"
              >搜索</el-button>
              <el-button
                icon="el-icon-refresh"
                @click="handleReset"
              >清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="list">
        <el-button
          type="primary"
          class="add"
          size="small"
          icon="el-icon-plus"
          @click="editMailbox"
        >新增</el-button>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          ref="table"
          highlight-current-row
          v-loading="loading"
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="风险编号"
            prop="fxCode"
            align="center"
          ></el-table-column>
          <el-table-column
            label="风险类型"
            prop="fxdlCode"
            align="center"
            :formatter="risktypeFormatter"
          ></el-table-column>
          <el-table-column
            label="风险名称"
            prop="fxxlName"
            align="center"
          ></el-table-column>
          <el-table-column
            label="风险等级"
            prop="fxdjCode"
            align="center"
            :formatter="riskLiveFormatter"
          ></el-table-column>
          <el-table-column
            label="状态"
            prop="qyStatus"
            align="center"
            :formatter="statusFormatter"
          >
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click.stop="handleUpdate(scope.row, 'check')"
              >查看</el-button>
              <el-button
                size="mini"
                type="text"
                @click.stop="handleUpdate(scope.row, 'edit')"
              >编辑</el-button>
              <!-- <el-button size="mini" type="text" @click.stop="handleUpdate(scope.row, 'forbidden')">{{scope.row.status == 0 ? '禁用' : '启用'}}</el-button> -->
              <el-button
                size="mini"
                type="text"
                @click.stop="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            @size-change="sizeChange"
            @current-change="pageChange"
            :current-page="pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    
    <el-dialog
      :title="title"
      :visible.sync="showFlag"
      width="40%"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center
    >
      <el-form
        ref="editForm"
        :rules="rule"
        :model="editForm"
        label-width="100px"
      >
        <el-row>
          <el-col
            :span="22"
            v-if="title !== '新增风险'"
          >
            <el-form-item label="风险编号">
              <el-input
                v-model="editForm.fxCode"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item
              prop="fxdlCode"
              label="风险类型"
            >
              <el-select
                style="width: 100%"
                v-model="editForm.fxdlCode"
                placeholder="请选择标风险类型"
                :disabled="title == '查看风险' || title == '编辑风险'"
              >
                <el-option
                  v-for="(dict, index) in signTypeOptionTwo"
                  :label="dict.label"
                  :value="dict.value"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item
              label="风险名称"
              prop="fxxlName"
            >
              <el-input
                v-model="editForm.fxxlName"
                placeholder="请输入风险名称"
                clearable
                :disabled="title == '查看风险'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item
              prop="fxdjCode"
              label="风险等级"
            >
              <el-select
                style="width: 100%"
                v-model="editForm.fxdjCode"
                placeholder="请选择标风险等级"
                :disabled="title == '查看风险'"
              >
                <el-option
                  v-for="(dict, index) in signLiveOptionTwo"
                  :label="dict.label"
                  :value="dict.value"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="备注">
              <el-input
                v-model="editForm.fxhy"
                type="textarea"
                placeholder="请输入备注"
                clearable
                :disabled="title == '查看风险'"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="22"
            v-if="title == '查看风险'"
          >
            <el-form-item label="状态">
              <el-input
                v-model="editForm.qyStatus"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            v-else
          >
            <el-form-item label="状态">
              <el-switch
                v-model="editForm.qyStatus"
                :active-text="editForm.qyStatus ? '已启用' : '已禁用'"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          @click="showFlag = false"
          :type="title == '查看风险' ? 'primary' : ''"
        >
          {{ title !== "查看风险" ? "取消" : "关闭" }}</el-button>
        <el-button
          type="primary"
          v-if="title !== '查看风险'"
          @click="handleSubmit"
        >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDict,
  jrfkFxyjConfig,
  newRiskInsent,
  riskUpdate,
  deleteRisk,
} from '@/api/riskWarning.js'
export default {
  data() {
    return {
      // 搜索表单
      formInline: {
        fxCode: '',
        fxdlCode: '',
        fxdjCode: '',
        fxxlName: '',
        qyStatus: '',
      },
      tableData: [],
      loading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      // 标签启用状态
      LabeStatusOption: [
        { label: '全部', value: '' },
        { label: '启用', value: '1' },
        { label: '禁用', value: '0' },
      ],
      //风险等级
      signLiveOption: [
        { label: '全部', value: '' },
        { label: '高', value: 0 },
        { label: '中', value: 1 },
        { label: '低', value: 2 },
      ],
      signLiveOptionTwo: [
        { label: '高', value: 0 },
        { label: '中', value: 1 },
        { label: '低', value: 2 },
      ],
      //风险类型
      signTypeOption: [],
      signTypeOptionTwo: [],
      //弹框
      title: '',
      showFlag: false,
      editForm: {
        fxCode: '',
        fxdlCode: '',
        fxdjCode: '',
        fxxlName: '',
        fxhy: '',
        qyStatus: 0,
        id: '',
      },
      rule: {
        fxdlCode: [
          { required: true, message: '请选择标风险类型', trigger: 'change' },
        ],
        fxxlName: [
          { required: true, message: '请输入风险名称', trigger: 'blur' },
        ],
        fxdjCode: [
          { required: true, message: '请选择标风险等级', trigger: 'change' },
        ],
      },
    }
  },
  created() {
    this.handleSearch()
    this.getFxlxData()
  },
  methods: {
    //下拉变化
    pageNumChang() {
      this.pageNum = 1
    },
    //HXFXDL 风险类型字典
    getFxlxData() {
      const params = {
        code: 'HXFXDL',
        page: '1',
        size: '50',
      }
      getDict(params).then((res) => {
        let dicOpt = res.data.records.map((item) => {
          return { label: item.fieldName, value: Number(item.fieldCode) }
        })
        this.signTypeOption = [{ label: '全部', value: '' }, ...dicOpt]
        this.signTypeOptionTwo = dicOpt
      })
    },
    //搜索
    handleSearch() {
      console.log(this.formInline.fxCode)
      this.loading = true
      let form = this.formInline
      let params = new FormData()
      if (form.fxCode !== '') {
        params.append('fxCode', form.fxCode)
      }
      if (form.fxxlName !== '') {
        params.append('fxxlName', form.fxxlName)
      }
      if (form.fxdlCode !== '') {
        params.append('fxdlCode', form.fxdlCode)
      }
      if (form.fxdjCode !== '') {
        params.append('fxdjCode', form.fxdjCode)
      }
      if (form.qyStatus !== '') {
        params.append('qyStatus', form.qyStatus)
      }
      params.append('pageNum', this.pageNum)
      params.append('pageSize', this.pageSize)
      jrfkFxyjConfig(params).then((res) => {
        if (res.operate == 'success') {
          this.tableData = res.data.records
          this.total = Number(res.data.total)
          this.loading = false
        }
      })
    },
    //清空
    handleReset() {
      ;(this.formInline.fxCode = ''),
        (this.formInline.fxdlCode = ''),
        (this.formInline.fxdjCode = ''),
        (this.formInline.fxxlName = ''),
        (this.formInline.qyStatus = ''),
        this.handleSearch()
    },
    sizeChange(val) {
      this.pageSize = val
      this.pageNum = 1
      this.handleSearch()
    },
    pageChange(val) {
      this.pageNum = val
      this.handleSearch()
    },
    //新增
    editMailbox() {
      this.showFlag = true
      this.title = '新增风险'
      this.editForm = {}
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
      })
    },
    //编辑
    handleUpdate(row, type) {
      console.log(row)
      this.showFlag = true
      this.title = type == 'check' ? '查看风险' : '编辑风险'
      this.editForm.fxCode = row.fxCode
      this.editForm.fxxlName = row.fxxlName
      this.editForm.fxdlCode = Number(row.fxdlCode)
      this.editForm.fxdjCode = Number(row.fxdjCode)
      this.editForm.fxhy = row.fxhy
      this.editForm.id = row.id
      if (type === 'check') {
        this.editForm.qyStatus = row.qyStatus == 1 ? '启用' : '禁用'
      } else {
        this.editForm.qyStatus = row.qyStatus == 1 ? true : false
      }
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
      })
    },
    //保存
    handleSubmit() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          let form = this.editForm
          let params = new FormData()
          params.append('fxCode', form.fxCode)
          params.append('fxxlName', form.fxxlName)
          params.append('fxdlCode', form.fxdlCode)
          params.append('fxdjCode', form.fxdjCode)
          params.append('fxhy', form.fxhy)
          params.append('qyStatus', form.qyStatus ? 1 : 0)
          console.log(params, 'params')
          if (this.title == '新增风险') {
            newRiskInsent(params).then((res) => {
              if (res.operate == 'success') {
                this.showFlag = false
                this.$message({
                  type: 'success',
                  message: res.message,
                })
                this.handleSearch()
              } else {
                this.showFlag = false
                this.$message({
                  type: 'success',
                  message: res.msg,
                })
              }
            })
          } else if (this.title == '编辑风险') {
            let paramsTwo = new FormData()
            paramsTwo.append('fxCode', form.fxCode)
            paramsTwo.append('fxxlName', form.fxxlName)
            paramsTwo.append('fxdlCode', form.fxdlCode)
            paramsTwo.append('fxdjCode', form.fxdjCode)
            paramsTwo.append('fxhy', form.fxhy)
            paramsTwo.append('qyStatus', form.qyStatus ? 1 : 0)
            paramsTwo.append('id', form.id)
            riskUpdate(paramsTwo).then((res) => {
              if (res.operate == 'success') {
                this.showFlag = false
                this.$message({
                  type: 'success',
                  message: res.message,
                })
                this.handleSearch()
              } else {
                this.showFlag = false
                this.$message({
                  type: 'success',
                  message: res.msg,
                })
              }
            })
          }
        }
      })
    },
    //删除
    handleDelete(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let params = { id: row.id }
        deleteRisk(params).then((res) => {
          if (res.operate == 'success') {
            this.$message({
              type: 'success',
              message: res.message,
            })
            this.handleSearch()
          } else {
            this.$message({
              type: 'error',
              message: res.message,
            })
          }
        })
      })
    },
    statusFormatter(r, c, v, i) {
      let list = this.LabeStatusOption.filter((item) => item.value == v)
      return list.length > 0 ? list[0].label : v
    },
    riskLiveFormatter(row) {
      let obj = ''
      this.signLiveOption.forEach((item) => {
        if (row.fxdjCode == item.value) {
          obj = item.label
        }
      })
      return obj
    },
    risktypeFormatter(row) {
      // let list = this.signTypeOptionTwo.filter((item) => item.value == v);
      // return list.length > 0 ? list[0].label : v;
      let obj = ''
      this.signTypeOption.forEach((item) => {
        if (row.fxdlCode == item.value) {
          obj = item.label
        }
      })
      return obj
    },
  },
}
</script>

<style scoped lang="scss">
.el-col-8 {
  height: 50px !important;
}
.search {
  padding: 20px 0 20px 20px;
  background: #fff;
}

.list {
  .add {
    margin-bottom: 10px;
  }
}

.formItem {
  width: 100%;

  /deep/.el-form-item__content {
    width: calc(100% - 125px);

    /deep/.el-select,
    /deep/.el-cascader {
      width: 100%;
    }

    .w100 {
      width: 100% !important;
    }
  }
}

.pagination {
  width: 100%;
  margin-top: 10px;
  display: flex;
  justify-content: center;
}

.dialog-footer {
  text-align: center;
}
</style>